package com.turnengine.client.global.announcement.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Announcement Serializer.
 */
public class AnnouncementSerializer implements IAnnouncementSerializer {

	@Override
	public IAnnouncement readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		int gameId = reader.readInt();
		String gameName = reader.readString(true);
		int versionId = reader.readInt();
		String versionName = reader.readString(true);
		int instanceId = reader.readInt();
		String instanceName = reader.readString(true);
		int userId = reader.readInt();
		String userName = reader.readString(true);
		long timestamp = reader.readLong();
		String subject = reader.readString(true);
		String body = reader.readString(true);

		// Create object
		IAnnouncement object = new Announcement();
		object.setId(id);
		object.setGameId(gameId);
		object.setGameName(gameName);
		object.setVersionId(versionId);
		object.setVersionName(versionName);
		object.setInstanceId(instanceId);
		object.setInstanceName(instanceName);
		object.setUserId(userId);
		object.setUserName(userName);
		object.setTimestamp(timestamp);
		object.setSubject(subject);
		object.setBody(body);
		return object;
	}

	public void writeObject(IDataWriter writer, IAnnouncement object) throws IOException {

		// Get fields
		int id = object.getId();
		int gameId = object.getGameId();
		String gameName = object.getGameName();
		int versionId = object.getVersionId();
		String versionName = object.getVersionName();
		int instanceId = object.getInstanceId();
		String instanceName = object.getInstanceName();
		int userId = object.getUserId();
		String userName = object.getUserName();
		long timestamp = object.getTimestamp();
		String subject = object.getSubject();
		String body = object.getBody();

		// Write fields
		writer.writeInt(id);
		writer.writeInt(gameId);
		writer.writeString(gameName, true);
		writer.writeInt(versionId);
		writer.writeString(versionName, true);
		writer.writeInt(instanceId);
		writer.writeString(instanceName, true);
		writer.writeInt(userId);
		writer.writeString(userName, true);
		writer.writeLong(timestamp);
		writer.writeString(subject, true);
		writer.writeString(body, true);
	}
}
